package com.work.keep.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.work.keep.R;
import com.work.keep.datas.SingleWorkoutTimeLineData;
import com.work.keep.volley.VolleySingleton;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/20.
 */
public class MySingleWorkoutAdapter extends BaseRecyclerAdapter {
    private static final int TYPE_HEADER = 0, TYPE_ITEM = 1, TYPE_FOOT = 2;
    private View headView;
    private boolean isAddFoot = false;
    private boolean isAddHead = false;
    private View footView;
    private int headViewSize = 0;
    private int footViewSize = 0;
    private Context context;
    private List<SingleWorkoutTimeLineData.DataEntity> timeLineLists;
    private VolleySingleton volleySingleton;
    private ImageLoader imageLoader;
    private int protect = 0;

    public MySingleWorkoutAdapter(Context context, View headView) {
        this.context = context;
        timeLineLists = new ArrayList<>();
        volleySingleton = VolleySingleton.getInstance();
        imageLoader = volleySingleton._getImageLoader();
        this.headView = headView;
        headViewSize = 1;
    }

    public void addTimeLineData(List<SingleWorkoutTimeLineData.DataEntity> timeLineLists) {
        this.timeLineLists.addAll(timeLineLists);

        notifyDataSetChanged();

    }



    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = null;
        switch (viewType) {
            case TYPE_HEADER:
                v = headView;
                return new MyViewHolder(v);
            case TYPE_ITEM:
                v = LayoutInflater.from(context).inflate(R.layout.item_five_singleworkout, parent, false);
                return new ItemFiveViewHolder(v);
            case TYPE_FOOT:
                v = footView;
                return new MyViewHolder(v);
        }
        return null;
//        if (viewType == 0) {
//            v = LayoutInflater.from(context).inflate(R.layout.item_one_singleworkout, parent, false);
//            return new ItemOneViewHolder(v);
//        }
//        if (viewType == 1) {
//            v = LayoutInflater.from(context).inflate(R.layout.item_two_singleworkout, parent, false);
//            return new ItemTwoViewHolder(v);
//        }
//        if (viewType == 2) {
//            v = LayoutInflater.from(context).inflate(R.layout.item_three_singleworkout, parent, false);
//            return new ItemFiveViewHolder(v);
//        }
//        if (viewType == 3) {
//            v = LayoutInflater.from(context).inflate(R.layout.item_four_singleworkout, parent, false);
//            return new ItemFourViewHolder(v);
//        }

    }

    @Override
    protected int getItemLayout() {
        return 0;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        public MyViewHolder(View itemView) {
            super(itemView);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
//        if (protect!=0){
//            switch (position) {
//                case 0:
//                    ItemOneViewHolder holder0 = ((ItemOneViewHolder) holder);
//                    holder0.singleWorkoutOneName.setText(mainDataLists.getData().getName());
//                    holder0.singleWorkoutOneDesc.setText(mainDataLists.getData().getDescription());
//                    if (mainDataLists.getData().getDifficulty() == 1) {
//                        holder0.singleWorkoutOneDiff.setText("K1 初学者");
//                    } else {
//                        holder0.singleWorkoutOneDiff.setText("K2 初学者");
//                    }
//                    holder0.singleWorkoutOnePoints.setText(mainDataLists.getData().getTrainingpoints().get(0).getName());
//                    holder0.singleWorkoutOneDuration.setText(mainDataLists.getData().getDuration() + "");
//                    holder0.singleWorkoutOneTimes.setText(mainDataLists.getData().getSteps().size() + "");
//                    holder0.singleWorkoutOneCalorie.setText(mainDataLists.getData().getCalorie() + "千卡");
//                    break;
//                case 1:
//                    ItemTwoViewHolder holder1 = ((ItemTwoViewHolder) holder);
//                    holder1.recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
////                holder1.recyclerView.setAdapter();
//                    break;
//                case 2:
//                    ItemThreeViewHolder holder2 = ((ItemThreeViewHolder) holder);
//                    holder2.singleWorkoutThreePeople.setText(mainDataLists.getData().getPioneer() + "");
//                    holder2.singleWorkoutThreeEquipment.setText(mainDataLists.getData().getEquipments().get(0).getName());
//                    holder2.singleWorkoutThreeJoin.setOnClickListener(new View.OnClickListener() {
//                        @Override
//                        public void onClick(View v) {
//                            Toast.makeText(context, "参加训练", Toast.LENGTH_SHORT).show();
//                        }
//                    });
//                    break;
//                case 3:
//                    ItemFourViewHolder holder3 = ((ItemFourViewHolder) holder);
//                    holder3.singleWorkoutFourTimes.setText(mainDataLists.getData().getTotalFinished() + "");
//                    holder3.singleWorkoutFourAvater.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
//                    holder3.singleWorkoutFourAvater.setAdapter(new MyItemFourRecyclerAdapter(context, pioneers));
//                    break;
//                default:

        if (position > headViewSize - 1 && position < headViewSize + timeLineLists.size()) {
            ItemFiveViewHolder holder4 = ((ItemFiveViewHolder) holder);
            holder4.singleWorkoutfiveItemAvater.setDefaultImageResId(R.mipmap.ic_launcher);
            holder4.singleWorkoutfiveItemAvater.setErrorImageResId(R.mipmap.ic_launcher);
            holder4.singleWorkoutfiveItemAvater.setImageUrl(timeLineLists.get(position - headViewSize).getAuthor().getAvatar(), imageLoader);
        }


    }

    @Override
    public int getItemCount() {
        return timeLineLists.size() + headViewSize + footViewSize;
    }

    @Override
    public int getItemViewType(int position) {
        int type = TYPE_ITEM;

        if (headViewSize == 1 && position == 0) {
            type = TYPE_HEADER;
        } else if (footViewSize == 1 && position == getItemCount() - 1) {
            //最后一个位置
            type = TYPE_FOOT;
        }
        return type;
    }

    class ItemOneViewHolder extends RecyclerView.ViewHolder {
        TextView singleWorkoutOneName, singleWorkoutOneDesc, singleWorkoutOneDiff, singleWorkoutOnePoints, singleWorkoutOneDuration, singleWorkoutOneTimes, singleWorkoutOneCalorie;

        public ItemOneViewHolder(View itemView) {
            super(itemView);
            singleWorkoutOneName = (TextView) itemView.findViewById(R.id.singleWorkoutOneName);
            singleWorkoutOneDesc = (TextView) itemView.findViewById(R.id.singleWorkoutOneDesc);
            singleWorkoutOneDiff = (TextView) itemView.findViewById(R.id.singleWorkoutOneDiff);
            singleWorkoutOnePoints = (TextView) itemView.findViewById(R.id.singleWorkoutOnePoints);
            singleWorkoutOneDuration = (TextView) itemView.findViewById(R.id.singleWorkoutOneDuration);
            singleWorkoutOneTimes = (TextView) itemView.findViewById(R.id.singleWorkoutOneTimes);
            singleWorkoutOneCalorie = (TextView) itemView.findViewById(R.id.singleWorkoutOneCalorie);
        }
    }

    class ItemTwoViewHolder extends RecyclerView.ViewHolder {
        RecyclerView recyclerView;

        public ItemTwoViewHolder(View itemView) {
            super(itemView);
//            recyclerView = (RecyclerView) itemView.findViewById(R.id.singleWorkoutItemTwo);
        }
    }

    class ItemThreeViewHolder extends RecyclerView.ViewHolder {
        TextView singleWorkoutThreePeople, singleWorkoutThreeEquipment;
        Button singleWorkoutThreeJoin;

        public ItemThreeViewHolder(View itemView) {
            super(itemView);
            singleWorkoutThreePeople = (TextView) itemView.findViewById(R.id.singleWorkoutThreePeople);
            singleWorkoutThreeEquipment = (TextView) itemView.findViewById(R.id.singleWorkoutThreeEquipment);
            singleWorkoutThreeJoin = (Button) itemView.findViewById(R.id.singleWorkoutThreeJoin);
        }
    }

    class ItemFourViewHolder extends RecyclerView.ViewHolder {
        TextView singleWorkoutFourTimes;
        RecyclerView singleWorkoutFourAvater;

        public ItemFourViewHolder(View itemView) {
            super(itemView);
            singleWorkoutFourTimes = (TextView) itemView.findViewById(R.id.singleWorkoutFourTimes);
            singleWorkoutFourAvater = (RecyclerView) itemView.findViewById(R.id.singleWorkoutFourAvater);
        }
    }

    class ItemFiveViewHolder extends RecyclerView.ViewHolder {
        NetworkImageView singleWorkoutfiveItemAvater;
        TextView singleWorkoutfiveItemName;

        public ItemFiveViewHolder(View itemView) {
            super(itemView);
            singleWorkoutfiveItemAvater = (NetworkImageView) itemView.findViewById(R.id.singleWorkoutfiveItemAvater);
            singleWorkoutfiveItemName = (TextView) itemView.findViewById(R.id.singleWorkoutfiveItemName);
        }
    }
}
